home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Appearance SDK 1.0.4 / Appearance Sample Code / Source / CDEFTester.cp < prev    next >
Encoding:
Text File  |  1999-07-16  |  13.1 KB  |  615 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        CDEFTester.cp
  3.  
  4.     Contains:    Code to demonstrate all types of controls.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (MAA)    Matt Ackeret
  21.         (edv)    Ed Voas
  22.  
  23.     Change History (most recent first):
  24.  
  25.          <7>    11/21/97    MAA        Add UserPane
  26.          <6>     11/7/97    MAA        Add tab Boolean support in DisplayPartCode
  27.          <5>     11/5/97    MAA        add Tabs
  28.          <4>    10/29/97    MAA        Make Activate and Deactivate enable and disable global font
  29.                                     menu, respectively
  30.          <3>    10/28/97    MAA        Add EditText/other controls/ChangeControlFont back in
  31.          <2>    10/28/97    edv        Use RadioGroup control!
  32.          <1>     9/11/97    edv        First checked in.
  33. */
  34.  
  35. //
  36. //    This file opens a dialog and adds a menu to the menu bar which allows the user
  37. //    to create and play with the different control types.
  38. //
  39.  
  40. #include <Dialogs.h>
  41. #include <TextUtils.h>
  42. #include "CDEFTester.h"
  43. #include "CDEFTesterUtils.h"
  44. #include "Appearance.h"
  45.  
  46. extern MenuHandle        gFontMenu;        // Menu used to choose a font
  47.  
  48. enum
  49. {
  50.     kBevelButton        = 1,
  51.     kChasingArrows        = 2,
  52.     kDisclosureTriangle    = 3,
  53.     kDivider            = 4,
  54.     kEditText            = 5,
  55.     kFinderHeader        = 6,
  56.     kGroupBox            = 7,
  57.     kIconCDEF            = 8,
  58.     kImageWell            = 9,
  59.     kLittleArrows        = 10,
  60.     kPictureCDEF        = 11,
  61.     kPlacard            = 12,
  62.     kPopupArrow            = 13,
  63.     kProgressBar        = 14,
  64.     kScrollBar            = 15,
  65.     kStaticText            = 16,
  66.     kTabs                = 17,
  67.     kUserPane            = 18,
  68.     kPushButton            = 19,
  69.     kCheckBox            = 20,
  70.     kRadioButton        = 21,
  71.     kSlider                = 22,
  72.     kClock                = 23
  73. };
  74.  
  75. enum
  76. {
  77.     kValueText            = 2,
  78.     kValueOK            = 3,
  79.     kValueCancel        = 4
  80. };
  81.  
  82. #define Height( r )        ( (r).bottom - (r).top )
  83. #define Width( r )        ( (r).right - (r).left )
  84.  
  85. MenuHandle CDEFTester::fMenu = nil;
  86.  
  87.  
  88. CDEFTester::CDEFTester() : BaseWindow( 130 )
  89. {
  90.     ControlRef        control;
  91.     Rect            bounds;
  92.     
  93.     if (fMenu == nil)
  94.         fMenu = GetMenu(133);
  95.         
  96.     fControl = nil;
  97.     fIsTab = false;
  98.  
  99.     fPartRect = fWindow->portRect;
  100.     
  101.     fPartRect.top = fPartRect.bottom - 20;
  102.     fPartRect.right = fPartRect.left + 260;
  103.     OffsetRect( &fPartRect, 5, -5 );
  104.     
  105.     CreateRootControl( fWindow, &control );
  106.     
  107.     bounds = fPartRect;
  108.     bounds.right = bounds.left + 60;
  109.     
  110.     OffsetRect( &bounds, 275, 0 );
  111.     fDisableButton = NewControl( fWindow, &bounds, "\pDisable", true, 0, 0, 1, kControlPushButtonProc, 0 );
  112.  
  113.     bounds.right = bounds.left + 70;
  114.     OffsetRect( &bounds, 70, 0 );
  115.     fSetValueButton = NewControl( fWindow, &bounds, "\pSet Value", true, 0, 0, 1, kControlPushButtonProc, 0 );
  116.  
  117.     DeactivateControl( fDisableButton );
  118.     DeactivateControl( fSetValueButton );
  119. }
  120.  
  121. CDEFTester::~CDEFTester()
  122. {
  123.     MenuHandle theMenu;
  124.     theMenu = GetMyMenu();
  125.     
  126.     if (theMenu)
  127.     {
  128.          DeleteMenu( (**theMenu).menuID );
  129.          InvalMenuBar();
  130.     }
  131.  
  132.     if ( fControl ) 
  133.         DisposeControl( fControl );
  134.  
  135.     if (gFontMenu)
  136.     {
  137.         DisableItem(gFontMenu, 0);
  138.         InvalMenuBar();
  139.     }
  140. }
  141.  
  142. void
  143. CDEFTester::Activate( EventRecord& event )
  144. {
  145.     ControlRef        root;
  146.     
  147.     BaseWindow::Activate( event );
  148.     
  149.     if ( GetRootControl( fWindow, &root ) == noErr )
  150.         ActivateControl( root );
  151.  
  152.     if ((gFontMenu) && (fControl))
  153.     {
  154.         EnableItem(gFontMenu, 0);
  155.         InvalMenuBar();
  156.     }
  157. }
  158.  
  159. void
  160. CDEFTester::Deactivate( EventRecord& event )
  161. {
  162.     ControlRef        root;
  163.     
  164.     BaseWindow::Deactivate( event );
  165.     
  166.     if ( GetRootControl( fWindow, &root ) == noErr )
  167.         DeactivateControl( root );
  168.         
  169.     if (gFontMenu)
  170.     {
  171.         DisableItem(gFontMenu, 0);
  172.         InvalMenuBar();
  173.     }
  174. }
  175.  
  176. void
  177. CDEFTester::Draw()
  178. {
  179.     DrawControls( fWindow );
  180. }
  181.  
  182. void
  183. CDEFTester::HandleClick( EventRecord& event )
  184. {
  185.     Point        where = event.where;
  186.     ControlRef    control;
  187.     SInt16        part;
  188.     
  189.     SetPort( fWindow );
  190.     GlobalToLocal( &where );
  191.     
  192.     part = FindControl( where, fWindow, &control );
  193.     
  194.     if ( control == fDisableButton )
  195.     {
  196.         if ( TrackControl( control, where, (ControlActionUPP)-1L ) )
  197.         {
  198.             if ( GetControlReference( control ) == 0 )
  199.             {
  200.                 DeactivateControl( fControl );
  201.                 SetControlTitle( control, "\pEnable" );
  202.                 SetControlReference( control, 1 );
  203.             }
  204.             else
  205.             {
  206.                 ActivateControl( fControl );
  207.                 SetControlTitle( control, "\pDisable" );
  208.                 SetControlReference( control, 0 );
  209.             }
  210.         }
  211.     }
  212.     else if ( control == fSetValueButton )
  213.     {
  214.         if ( TrackControl( control, where, (ControlActionUPP)-1L ) )
  215.         {
  216.             SetValue();
  217.         }
  218.     }
  219.     else
  220.     {
  221.         DisplayPartCode( part );
  222.         
  223.         if ( part )
  224.         {
  225.             part = TrackControl( control, where, (ControlActionUPP)-1L );
  226.             DisplayPartCode( part );
  227.         }
  228.     }
  229. }
  230.  
  231.  
  232.  
  233. //————————————————————————————————————————————————————————————————————————————————
  234. //    • HandleKeyDown
  235. //————————————————————————————————————————————————————————————————————————————————
  236. //    Call HandleControlKey to give the key to the control
  237. //
  238.  
  239. void CDEFTester::HandleKeyDown(EventRecord &event)
  240. {
  241.     HandleControlKey(this->fControl, (event.message & keyCodeMask)>>16, event.message & charCodeMask, event.modifiers);
  242. }
  243.  
  244.  
  245. //————————————————————————————————————————————————————————————————————————————————
  246. //    • Idle
  247. //————————————————————————————————————————————————————————————————————————————————
  248. //    Call IdleControls to give time to controls like edit text, chasing arrows,
  249. //    and the indeterminate progress indicator.
  250. //
  251. void
  252. CDEFTester::Idle()
  253. {
  254.     IdleControls( fWindow );
  255. }
  256.  
  257. void CDEFTester::ChangeControlFont(SInt16 menuID, SInt16 itemNo)
  258. {
  259.     ControlFontStyleRec fontStyleRec;
  260.     MenuHandle theMenu = GetMenuHandle(menuID);
  261.     SInt16 fontID;
  262.     OSErr theErr;
  263.     Rect bounds;
  264.     SInt16            baseLine;
  265.     
  266.     if (fControl)
  267.     {
  268.         GetMenuItemFontID(theMenu, itemNo, &fontID);    
  269.         fontStyleRec.flags = kControlUseFontMask;
  270.         fontStyleRec.font = fontID;
  271.         theErr = SetControlFontStyle(fControl, &fontStyleRec);
  272.  
  273.         HideControl(fControl); // the rect is going to change.. want to erase old rect..
  274.         // reset the font bounds since we changed the size..
  275.         if ( GetBestControlRect( fControl, &bounds, &baseLine ) == noErr )
  276.             (**fControl).contrlRect = bounds;
  277.  
  278.         ShowControl(fControl);
  279.     }
  280. }
  281.  
  282.     
  283.  
  284. //————————————————————————————————————————————————————————————————————————————————
  285. //    • HandleMenuSelection
  286. //————————————————————————————————————————————————————————————————————————————————
  287. //    Create the right type of control. Ignore any menu items not from our menu.
  288. //
  289. void
  290. CDEFTester::HandleMenuSelection( SInt16 menuID, SInt16 itemNo )
  291. {
  292.     ControlRef        newControl = nil;
  293.     
  294.     if ((menuID != (**GetMyMenu()).menuID) && (menuID != kMenuFonts)) return;
  295.     
  296.     if (menuID == kMenuFonts)
  297.     {
  298.         ChangeControlFont(menuID, itemNo);
  299.         return;
  300.     }
  301.     
  302.     fIsTab = false;
  303.     
  304.     switch ( itemNo )
  305.     {
  306.         case kTabs:
  307.             newControl = CreateTabs( fWindow );
  308.             if (newControl)
  309.                 fIsTab = true;
  310.             break;
  311.             
  312.         case kUserPane:
  313.             newControl = CreateUserPane( fWindow );
  314.             break;
  315.             
  316.           case kStaticText:
  317.               newControl = CreateStaticText( fWindow );
  318.               break;
  319.  
  320.           case kEditText:
  321.               newControl = CreateEditText( fWindow );
  322.               break;
  323.  
  324.         case kPushButton:
  325.             newControl = CreatePushButton( fWindow );
  326.             break;
  327.         
  328.         case kCheckBox:
  329.             newControl = CreateCheckBox( fWindow );
  330.             break;
  331.         
  332.         case kSlider:
  333.             newControl = CreateSlider( fWindow );
  334.             break;
  335.         
  336.         case kClock:
  337.             newControl = CreateClock( fWindow );
  338.             break;
  339.         
  340.         case kRadioButton:
  341.             newControl = CreateRadioButton( fWindow );
  342.             break;
  343.             
  344.         case kBevelButton:
  345.             newControl = CreateBevelButton( fWindow );
  346.             break;
  347.  
  348.         case kChasingArrows:
  349.             newControl = CreateChasingArrows( fWindow );
  350.             break;
  351.  
  352.         case kDivider:
  353.             newControl = CreateDivider( fWindow );
  354.             break;
  355.  
  356.         case kDisclosureTriangle:
  357.             newControl = CreateTriangle( fWindow );
  358.             break;
  359.  
  360.         case kFinderHeader:
  361.             newControl = CreateFinderHeader( fWindow );
  362.             break;
  363.  
  364.         case kIconCDEF:
  365.             newControl = CreateIconCDEF( fWindow );
  366.             break;
  367.  
  368.         case kPictureCDEF:
  369.             newControl = CreatePictureCDEF( fWindow );
  370.             break;
  371.  
  372.         case kProgressBar:
  373.             newControl = CreateProgressBar( fWindow );
  374.             break;
  375.  
  376.         case kLittleArrows:
  377.             newControl = CreateLittleArrows( fWindow );
  378.             break;
  379.  
  380.         case kGroupBox:
  381.             newControl = CreateGroupBox( fWindow );
  382.             break;
  383.  
  384.         case kPlacard:
  385.             newControl = CreatePlacard( fWindow );
  386.             break;
  387.  
  388.         case kPopupArrow:
  389.             newControl = CreatePopupArrow( fWindow );
  390.             break;
  391.  
  392.         case kScrollBar:
  393.             newControl = CreateScrollBar( fWindow );
  394.             break;
  395.  
  396.         case kImageWell:
  397.             newControl = CreateImageWell( fWindow );
  398.             break;
  399.     }
  400.  
  401.     if ( newControl )
  402.     {
  403.         if ( fControl ) DisposeControl( fControl );
  404.         fControl = newControl;
  405.         CenterControlInWindow();
  406.         ShowControl( fControl );
  407.         
  408.         SetControlTitle( fDisableButton, "\pDisable" );
  409.         SetControlReference( fDisableButton, 0 );
  410.         ActivateControl( fDisableButton );
  411.         ActivateControl( fSetValueButton );
  412.         
  413.         //now the WYSIWYG menu can be used
  414.         if (gFontMenu)
  415.         {
  416.             EnableItem(gFontMenu, 0);
  417.             InvalMenuBar();
  418.         }
  419.     }
  420.     else if ( fControl == nil )
  421.     {
  422.         SetControlTitle( fDisableButton, "\pDisable" );
  423.         SetControlReference( fDisableButton, 0 );
  424.         DeactivateControl( fDisableButton );
  425.         DeactivateControl( fSetValueButton );
  426.     }
  427. }
  428.  
  429. //————————————————————————————————————————————————————————————————————————————————
  430. //    • CenterControlInWindow
  431. //————————————————————————————————————————————————————————————————————————————————
  432. //    Moves the control into the center of the window.
  433. //
  434. void
  435. CDEFTester::CenterControlInWindow()
  436. {
  437.     Rect        bounds;
  438.     SInt16        left, top, height, width;
  439.     
  440.     if ( fControl == nil ) return;
  441.     
  442.     bounds = (**fControl).contrlRect;
  443.     
  444.     height = Height( bounds );
  445.     width = Width( bounds );
  446.     
  447.     left = (( Width( fWindow->portRect ) - width ) / 2 ) + fWindow->portRect.left;
  448.     top = (( Height( fWindow->portRect ) - height ) / 2 ) + fWindow->portRect.top;
  449.     
  450.     SetRect( &bounds, left, top, left + width, top + height );
  451.     (**fControl).contrlRect = bounds;
  452. }
  453.  
  454. //————————————————————————————————————————————————————————————————————————————————
  455. //    • SetValue
  456. //————————————————————————————————————————————————————————————————————————————————
  457. //    Allows the user to choose a specific value for the control.
  458. //
  459. void
  460. CDEFTester::SetValue()
  461. {
  462.     DialogPtr        dialog;
  463.     SInt16            itemNo;
  464.     Str255            text;
  465.     SInt32            number;
  466.     ControlKeyFilterUPP    filterProc;
  467.     ControlHandle    control;
  468.     
  469.     dialog = GetNewDialog( 2004, nil, (WindowRef)-1L );
  470.     if ( dialog == nil ) return;
  471.     
  472.     SetDialogDefaultItem( dialog, kValueOK );
  473.     SetDialogCancelItem( dialog, kValueCancel );
  474.     
  475.         // Set the text item to the current control value
  476.     GetDialogItemAsControl( dialog, kValueText, &control );
  477.     NumToString( GetControlValue( fControl ), text );
  478.     SetDialogItemText( (Handle)control, text );
  479.     
  480.         // make sure its selected
  481.     SelectDialogItemText( dialog, kValueText, 0, 32767 );
  482.     
  483.         // add our simple numbers-only key filter
  484.     filterProc = NewControlKeyFilterProc( NumericFilter );
  485.     SetControlData( control, 0, kControlEditTextKeyFilterTag, sizeof( filterProc ),
  486.                     (Ptr)&filterProc );
  487.  
  488.     itemNo = 0;
  489.     while( itemNo != kValueCancel && itemNo != kValueOK )
  490.     {
  491.         ModalDialog( nil, &itemNo );
  492.     }
  493.     DisposeRoutineDescriptor( filterProc );
  494.     
  495.     if ( itemNo == kValueCancel )
  496.     {
  497.         DisposeDialog( dialog );
  498.         return;
  499.     }
  500.         
  501.         // Get the text
  502.     GetDialogItemAsControl( dialog, kValueText, &control );
  503.     GetDialogItemText( (Handle)control, text );
  504.     StringToNum( text, &number );
  505.     
  506.     DisposeDialog( dialog );
  507.     
  508.     SetControlValue( fControl, number );
  509. }
  510.  
  511. //————————————————————————————————————————————————————————————————————————————————
  512. //    • DisplayPartCode
  513. //————————————————————————————————————————————————————————————————————————————————
  514. //    Prints the constant for the part code passed in.
  515. //
  516. void
  517. CDEFTester::DisplayPartCode( SInt16 part )
  518. {
  519.     Str255        lastString;
  520.     
  521.     SetPort( fWindow );
  522.     
  523.     EraseRect( &fPartRect );
  524.     MoveTo( fPartRect.left, fPartRect.bottom - 4 );
  525.     
  526.     DrawString( "\pLast Part: " );
  527.     NumToString( part, lastString );
  528.     DrawString( lastString );
  529.     
  530.     if (!fIsTab) 
  531.     // Tabs return part codes corresponding to the new tab you're pushing on/selecting.  The part numbers
  532.     // don't represent the standard part numbers as shown below.
  533.     {
  534.         DrawString( "\p (" );
  535.         switch ( part )
  536.         {
  537.             case kControlNoPart:
  538.                 DrawString( "\pkControlNoPart" );
  539.                 break;
  540.                 
  541.             case kControlClockPart:
  542.                 DrawString( "\pkControlClockPart" );
  543.                 break;
  544.                 
  545.             case kControlLabelPart:
  546.                 DrawString( "\pkControlLabelPart" );
  547.                 break;
  548.     
  549.             case kControlMenuPart:
  550.                 DrawString( "\pkControlMenuPart" );
  551.                 break;
  552.     
  553.             case kControlEditTextPart:
  554.                 DrawString( "\pkControlEditTextPart" );
  555.                 break;
  556.     
  557.             case kControlIconPart:
  558.                 DrawString( "\pkControlIconPart" );
  559.                 break;
  560.     
  561.             case kControlPicturePart:
  562.                 DrawString( "\pkControlPicturePart" );
  563.                 break;
  564.     
  565.             case kControlTrianglePart:
  566.                 DrawString( "\pkControlTrianglePart" );
  567.                 break;
  568.     
  569.             case kControlButtonPart:
  570.                 DrawString( "\pkControlButtonPart" );
  571.                 break;
  572.     
  573.             case kControlCheckBoxPart:
  574.                 DrawString( "\pkControlCheckBoxPart" );
  575.                 break;
  576.     
  577.             case kControlUpButtonPart:
  578.                 DrawString( "\pkControlUpButtonPart" );
  579.                 break;
  580.     
  581.             case kControlDownButtonPart:
  582.                 DrawString( "\pkControlDownButtonPart" );
  583.                 break;
  584.     
  585.             case kControlPageUpPart:
  586.                 DrawString( "\pkControlPageUpPart" );
  587.                 break;
  588.     
  589.             case kControlPageDownPart:
  590.                 DrawString( "\pkControlPageDownPart" );
  591.                 break;
  592.     
  593.             case kControlIndicatorPart:
  594.                 DrawString( "\pkControlIndicatorPart" );
  595.                 break;
  596.     
  597.             case kControlDisabledPart:
  598.                 DrawString( "\pkControlInactivePart" );
  599.                 break;
  600.     
  601.             case kControlInactivePart:
  602.                 DrawString( "\pkControlInactivePart" );
  603.                 break;
  604.     
  605.         }
  606.         DrawString( "\p)" );
  607.     }
  608. }
  609.  
  610. MenuHandle CDEFTester::GetMyMenu(void)
  611. {
  612.     return(fMenu);
  613. }
  614.  
  615.